function search(){
	$("#results").show();
	local.clearResults();
	map.clearOverlays();
	var location=document.getElementById("txt-search").value;
	if(location==""){
		document.getElementById("results").innerHTML = "<span style='margin:2px;color:#F00;'>请输入查询条件!</span>";
		return;
	}
	local.search(location);
}


var options = {
  onSearchComplete: function(results){
    // 
    if (local.getStatus() == BMAP_STATUS_SUCCESS){
        var s = [];
        s.push('<div style="font-family: arial,sans-serif; font-size: 12px;">');
        s.push('<div style="background: none repeat scroll 0% 0% rgb(255, 255, 255);">');
        s.push('<ol style="list-style: none outside none; padding: 0pt; margin: 0pt;">');
        openInfoWinFuns = [];

		//
		var marker_points=[];
        
        for (var i = 0; i < results.getCurrentNumPois(); i ++){
        	//(function(){
        		var index=i;
        		var marker = addMarker(results.getPoi(index).point,index);
        		marker_points.push(results.getPoi(index).point);
        		var openInfoWinFun = addInfoWindow(marker,results.getPoi(index),i);
        		openInfoWinFuns.push(openInfoWinFun);
        		// 
        		
        		var selected = "";
        		if(index == 0)
        		{
        			selected = "background-color:#AAA;";
        			openInfoWinFun();
        		}
        		s.push('<li id="list' + index + '" style="margin: 2px 0pt; padding: 0pt 5px 0pt 3px; cursor: pointer; overflow: hidden; line-height: 17px;' + selected + '" onclick="openInfoWinFuns[' + index + ']()">');
        		s.push('<span style="width:11px;height:11px;background:url(http://api.map.baidu.com/bmap/red_labels.gif) 0 ' + ( 2 - index*20 ) + 'px no-repeat;padding-left:10px;margin-right:3px"></span>');
        		s.push('<span style="color:#00c;text-decoration:underline">' + results.getPoi(index).title.replace(new RegExp(results.keyword,"g"),'<b>' + results.keyword + '</b>') + '</span>');
        		s.push('<span style="color:#666;"> - ' + results.getPoi(index).address + '</span><br/>');
        		if(index==0){
        			s.push('<span id="list'+index+'-add" style="width:100px;margin-top:15px;text-align:right;"><a href="javascript:openSchadule()" class="basic">添加日程</a></span>');
        		}
        		else{
        			s.push('<span id="list'+index+'-add" style="width:100px;margin-top:5px;display:none;text-align:right;"><a href="javascript:openSchadule()" class="basic">添加日程</a></span>');
        		}
        		s.push('</li>');
        		s.push('');
        		
        		
        	//})();
        	
        }
        //
        map.setViewport(marker_points);
        
        var current_index=results.getPageIndex();
		var page_num=results.getNumPages();
		
        //document.getElementById("test").value=current_index;
		if(current_index>0)
		{
			s.push('<a href = "javascript:void(0)" onclick="pageup('+current_index+')">上一页</a>')
		}
    if (current_index<page_num-1)
		{
			s.push('<a href = "javascript:void(0)" onclick="pagedown('+current_index+')">下一页</a>')
		}
    s.push('</ol></div></div>');
    document.getElementById("results").innerHTML = s.join("");
    }
    else if(local.getStatus() == BMAP_STATUS_TIMEOUT){
    	document.getElementById("results").innerHTML = "<span style='color:#F00;'>连接超时，请检查网络是否通畅.</span>";
    }
    else if(local.getStatus() ==BMAP_STATUS_UNKNOWN_LOCATION){
    	document.getElementById("results").innerHTML = "<span style='color:#F00;'>未找到符合的地点，请换个地点重试.</span>";
    }
    else{
    	document.getElementById("results").innerHTML = "<span style='color:#F00;'>发生错误，请重试.</span>";
    }
  }
};

function pagedown(index){
	map.clearOverlays();
	local.gotoPage(index+1);
}

function pageup(index){
	map.clearOverlays();
	local.gotoPage(index-1)
}

// 
function addMarker(point, index){
  var myIcon = new BMap.Icon("http://api.map.baidu.com/img/markers.png", new BMap.Size(23, 25), {
    offset: new BMap.Size(10, 25),
    imageOffset: new BMap.Size(0, 0 - index * 25)
  });
  var marker = new BMap.Marker(point, {icon: myIcon});
  map.addOverlay(marker);
  return marker;
}
// 
function addInfoWindow(marker,poi,index){
    var maxLen = 10;
    var name = null;
    if(poi.type == BMAP_POI_TYPE_NORMAL){
        name = "地址"
    }else if(poi.type == BMAP_POI_TYPE_BUSSTOP){
        name = "公交 "
    }else if(poi.type == BMAP_POI_TYPE_SUBSTOP){
        name = "地铁"
    }
    // infowindow
    var infoWindowTitle = '<div style="font-weight:bold;color:#CE5521;font-size:14px">'+poi.title+'</div>';
    // infowindow
    var infoWindowHtml = [];
    infoWindowHtml.push('<table cellspacing="0" style="table-layout:fixed;width:100%;font:12px arial,simsun,sans-serif;"><tbody>');
    infoWindowHtml.push('<tr>');
    if(name != null){
        infoWindowHtml.push('<td style="vertical-align:top;line-height:16px;width:38px;white-space:nowrap;word-break:keep-all">' + name + '</td>');
    }
    infoWindowHtml.push('<td style="vertical-align:top;line-height:16px">' + poi.address + ' </td>');
    infoWindowHtml.push('</tr>');
    infoWindowHtml.push('</tbody></table>');
    infoWindowHtml.push('<div style="width:98%;text-align:right;"><a style="text-decoration:none;color:#638DC4;" href="javascript:openSchadule()" class="basic">添加日程</a></div>');
    var infoWindow = new BMap.InfoWindow(infoWindowHtml.join(""),{title:infoWindowTitle,width:200}); 
    var openInfoWinFun = function(){
    	pushAddrInfo(poi.title,poi.point,poi.address);
        marker.openInfoWindow(infoWindow);
        
        for(var cnt = 0; cnt < maxLen; cnt++){ //搜索结果，选中则背景变色
            if(!document.getElementById("list" + cnt)){continue;}
            if(cnt == index){
                document.getElementById("list" + cnt).style.backgroundColor = "#AAA";
                document.getElementById("list" + cnt+"-add").style.display = "";
                //$("#list"+index+"-add").show();
            }else{
                document.getElementById("list" + cnt).style.backgroundColor = "#fff";
                document.getElementById("list" + cnt+"-add").style.display = "none";
                //$("#list"+index+"-add").hide();
            }
        }
    }
    marker.addEventListener("click", openInfoWinFun);
    return openInfoWinFun;
}

//添加选取的地址信息
function pushAddrInfo(title,point,address){
	 var html='<input type="hidden" id="location_data" value="'+point.lng+'|'+point.lat+'|'+title+'|'+address+'">';
	 //传回值的格式为：x,y,title,address
	 var view='<span title='+address+'>'+title+'</span>';
	 document.getElementById("location").innerHTML=view+html;
}
